package cn.tedu.v6;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {
     private static final Integer PORT =7878;
     private static final String HOST="127.0.0.1";
     private Socket socket;
     private BufferedReader br;
     private PrintWriter pw;
     private String ip;


     public Client () {
        try {
           socket = new Socket(HOST, PORT);
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
        }  catch (UnknownHostException e)  {
            System.out.println("IP传入错误了");
        }  catch (IOException e)  {
            System.out.println("客户端链接服务器端：IO流操作出现错误或者服务器端未启动呢");
        }
    }  public static void main(String[] args) {
        new Client().start();
    }public void start() {
        try {

             System.out.println("------欢迎使用美立大王牌聊天室------");
             Scanner input = new Scanner(System.in);
            String serverMessage = br.readLine();
             System.out.println(serverMessage);
            String username = input.nextLine();
             pw.println(username);
             serverMessage = br.readLine();
              System.out.println(serverMessage);
             String password = input.nextLine();
             pw.println(password);
             serverMessage = br.readLine();
             if  (serverMessage.startsWith("登录成功")) {
                 System.out.println("登录成功，欢迎进入聊天室");
                 chat();
             }
        }  catch (NullPointerException e)  {
             System.out.println("IP传入错误导致链接失败，可能是端口号错误");
        }  catch (IOException e) {
            System.out.println("服务器端断开连接");
        }  finally  {
            Close();
        }

    }

    private void chat()  {
        new Thread ( ()  ->  {
            try  {
                String message;
                while  ((message = br.readLine()) != null)  {
                     System.out.println("服务器端说： " + message);
                      System.out.println("请客户端输入：");
                     if (message.equalsIgnoreCase("exit")) {  //关写
                     System.out.println("客户端退出聊天...");
                       //关闭
                        break;
                    }
                }
            }  catch (IOException e)  {
                 System.out.println("服务器端读入失败了");
            }
         }).start();
         Scanner input = new Scanner(System.in);
         while (true)  {
             System.out.println("请客户端输入：");
             String line = input.nextLine();
             pw.println("客户端说：" + line);
             if  (line.equalsIgnoreCase("exit")) {
                 System.out.println("客户端退出聊天...");

                 System.exit(0);
                 break;//跳出循环

            }
        }
    }
    //封装一个针对关闭的功能
     private void Close() {
        if  (br != null)
            try  {
                 br.close();
            }  catch (IOException e) {
                 System.out.println("客户端通讯关闭失败");
            }

        if  (pw != null)
           pw.close();
        if  (socket != null && !socket.isClosed()) {
            try  {
                 socket.close();
            }  catch (IOException e) {
                 System.out.println("客户端通讯关闭失败");
            }
        }
    }
}

